public class MyKadane {
    public static int findMaxSubarraySum(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0; // 空数组特殊处理
        }
        int currentSum = arr[0];
        int maxSum = arr[0];
        for (int i = 1; i < arr.length; i++) {
            // 选择当前元素或当前子数组和 + 当前元素
            currentSum = Math.max(arr[i], currentSum + arr[i]);
            // 更新最大和
            maxSum = Math.max(maxSum, currentSum);
        }
        return maxSum;
    }

    public static void main(String[] args) {

        int[] test1 = {1, -2, 3, 5, -1};
        int[] test2 = {1, -2, 3, -8, 5, 1};
        int[] test3 = {1, -2, 3, -2, 5, 1};
        System.out.println(findMaxSubarraySum(test1));
        System.out.println(findMaxSubarraySum(test2));
        System.out.println(findMaxSubarraySum(test3));
    }
}